package org.jmc.refmanager.web.wicket.page.service;

import java.util.Set;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;

import org.apache.wicket.PageParameters;
import org.apache.wicket.authorization.strategies.role.annotations.AuthorizeInstantiation;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.jmc.refmanager.domain.model.OrgUnit;
import org.jmc.refmanager.domain.model.User;
import org.jmc.refmanager.domain.service.ChangeManager;
import org.jmc.refmanager.domain.service.OrgUnitManager;
import org.jmc.refmanager.domain.service.UserManager;
import org.jmc.refmanager.domain.service.exception.BusinessException;
import org.jmc.refmanager.security.SecurityHelper;
import org.jmc.refmanager.web.wicket.form.ServiceDeleteForm;
import org.jmc.refmanager.web.wicket.page.search.ServicesListPage;
import org.jmc.refmanager.web.wicket.page.template.TemplatePage;

/**
 */
@AuthorizeInstantiation(value={"ROLE_MODIF", "ROLE_APPROVER"})
public class ServiceDeletePage extends TemplatePage {
	@SpringBean(name="orgUnitManager")
	private OrgUnitManager orgUnitManager;

	@SpringBean(name="taskManager")
	private ChangeManager taskManager;
	
	@SpringBean(name="userManager")
	private UserManager userManager;

	/**
	 * @param parameters
	 */
	public ServiceDeletePage(PageParameters parameters) {
		super(parameters);
		initComponents(parameters);
	}

	private void initComponents(PageParameters parameters) {
		add(new Label("pageTitle", "Suppression du service"));

		final String matricule = SecurityHelper.getAuthentication().getName();
		
		Integer id = parameters.getAsInteger("id");
		OrgUnit deleteService = orgUnitManager.findById(id);
		final IModel<OrgUnit> serviceModel = new Model<OrgUnit>(deleteService);
		
        // deleteForm
        ServiceDeleteForm deleteForm = new ServiceDeleteForm("deleteForm", new CompoundPropertyModel<OrgUnit>(serviceModel)) {
			private static final long serialVersionUID = 1L;

			/* (non-Javadoc)
			 * @see org.apache.wicket.markup.html.form.Form#onSubmit()
			 */
			@Override
			protected void onSubmit() {
				try {
					OrgUnit deletedService = getModelObject();
					deletedService.setSupprime(true);
					
					// Appel e la couche de service
					User user = userManager.getByMatricule(matricule);
					taskManager.assignDeleteService(deletedService, user);
					
					// Affiche le message sur la page redirigee
					getSession().info("La suppression du service a été prise en compte et a été soumise à l'approbation du référent de direction.");
					setResponsePage(ServicesListPage.class);
				} catch (ConstraintViolationException e) {
					error("La demande de suppression de service n'a pas été prise en compte.");
					Set<ConstraintViolation<?>> violations = e.getConstraintViolations();
					for (ConstraintViolation<?> constraintViolation : violations) {
						error(constraintViolation.getMessage());
					}
					setResponsePage(ServiceDeletePage.this);
				} catch (BusinessException be) {
					error(be.getMessage());
					setResponsePage(ServiceDeletePage.this);
				}
			}
        };
        add(deleteForm);
	}
}

